
class RomanConverter:
    """
    实现罗马数字与整数的双向转换
    支持范围：1-3999
    """
    def __init__(self):
        self.num_map = [
            (1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'),
            (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'),
            (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')
        ]
        self.roman_map = {
            'I': 1, 'V': 5, 'X': 10, 'L': 50,
            'C': 100, 'D': 500, 'M': 1000
        }

    def int_to_roman(self, num: int) -> str:
        """ 整数转罗马数字 """
        if not 1 <= num <= 3999:
            raise ValueError("输入范围应为1-3999")
        
        result = []
        for value, symbol in self.num_map:
            while num >= value:
                result.append(symbol)
                num -= value
        return ''.join(result)

    def roman_to_int(self, s: str) -> int:
        """ 罗马数字转整数 """
        total = 0
        prev_value = 0
        
        for char in reversed(s):
            value = self.roman_map[char]
            if value < prev_value:
                total -= value
            else:
                total += value
            prev_value = value
        return total

if __name__ == "__main__":
    converter = RomanConverter()
    
    # 测试用例
    test_cases = [3, 4, 9, 58, 1994, "III", "IV", "IX", "LVIII", "MCMXCIV"]
    
    for case in test_cases:
        if isinstance(case, int):
            print(f"{case} -> {converter.int_to_roman(case)}")
        else:
            print(f"{case} -> {converter.roman_to_int(case)}")
